package crm.utility;


import java.util.HashMap;

import javax.swing.JComboBox;

import crm.daoInterfaces.CampaignDao;
import crm.daoInterfaces.InfoSourceDao;
import crm.daoInterfaces.InquiryStateDao;
import crm.daoInterfaces.PriorityDao;
import crm.daoInterfaces.ProductDao;
import crm.daoInterfaces.SalesPersonDao;
import crm.dto.InfoSource;
import crm.dto.InquiryState;
import crm.dto.Priority;
import crm.dto.Product;
import crm.dto.SalesPerson;
import crm.implementor.CampaignImp;
import crm.implementor.InfoSourceImp;
import crm.implementor.InquiryStateImp;
import crm.implementor.PriorityImp;
import crm.implementor.ProductImp;
import crm.implementor.SalesPersonImp;
import crm.model.Campaign;


@SuppressWarnings("unused")
public class AppUtility {
	
	
	
	public static final int ACTIVE =1;
	public static final int INACTIVE = 2;
	
	private static HashMap<String,Integer>SalesPerson=null;
	private static HashMap<String,Integer>InquiryState=null;
	private static HashMap<String,String>Product=null;
	private static HashMap<String,Integer>InfoSource=null;
	private static HashMap<String,Integer>Campaign=null;
	
	
	
	public static void setComboReq(JComboBox comboBox) throws Exception
	{
		ProductDao p=new ProductImp();
		Product[] product=p.retrieveAll();
		String[] productNames=new String[product.length+1];
		productNames[0]="--Select Product--";
		for(int i=1;i<=product.length;i++)
		{
			productNames[i]=product[i-1].getProductName();
		}
		ViewUtilites.set_comboBox(comboBox,productNames);
		
	}
	
	
	public static void setComboInfo(JComboBox comboBox) throws Exception
	{
		InfoSourceDao is=new InfoSourceImp();
		InfoSource[] infosource=is.retrieveAll();
		String[] items=new String[infosource.length+1];
		items[0]="--Select Source of Information--";
		for(int i=1;i<=infosource.length;i++)
		{
			items[i]=infosource[i-1].getInfoSourceName();
		}
		ViewUtilites.set_comboBox(comboBox, items);
		
	}
	
	public static void setComboStatus(JComboBox comboBox) throws Exception
	{
		InquiryStateDao status=new InquiryStateImp();
		InquiryState[] s=status.retrieveAll();
		String[] items=new String[s.length+1];
		items[0]="--Select Status--";
		for(int i=1;i<=s.length;i++)
		{
			items[i]=s[i-1].getStateName();
		}
		ViewUtilites.set_comboBox(comboBox, items);
		
		
	}
	
	public static void setComboAtten(JComboBox comboBox) throws Exception
	{
		SalesPersonDao att=new SalesPersonImp();
		SalesPerson[] a=att.retrieveAll();
		String[] items=new String[a.length+1];
		items[0]="--Select Sales Person--";
		for(int i=1;i<=a.length;i++)
		{
			items[i]=a[i-1].getSalesPersonName();
		}
		ViewUtilites.set_comboBox(comboBox, items);
		
	}
	
	public static void setComboPriority(JComboBox comboBox) throws Exception
	{
		PriorityDao att=new PriorityImp();
		Priority[] a=att.retrieveAll();
		String[] items=new String[a.length+1];
		items[0]="--Select Priority--";
		for(int i=1;i<=a.length;i++)
		{
			items[i]=a[i-1].getPriorityName();
		}
		ViewUtilites.set_comboBox(comboBox, items);
		
	}
	
/*--------------------------------------------------DATABSE UTILITY-----------------------------------------------------------------*/
	
	public static int getImquiryStateId(String inquiryStateName)
	{
		if(InquiryState == null)
		{
			InquiryState = new HashMap<String, Integer>();
			InquiryStateDao dao = new InquiryStateImp();
			InquiryState[] allInquiryState = null;
			try {
				allInquiryState = dao.retrieveAll();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
			for(int i =0;i<allInquiryState.length;i++)
			{
				InquiryState temp=allInquiryState[i];
				int value=temp.getAutoStateId();
				String key =temp.getStateName();
				InquiryState.put(key, value);
			}
		}
		System.out.println("The Inquiry State ID IS  "+InquiryState.get(inquiryStateName) +"The Inquiry State Value is "+inquiryStateName);
		return InquiryState.get(inquiryStateName);
	}
	
	public static int getSalesPersonId(String salesPersonName)
	{
		if(SalesPerson == null)
		{
			SalesPerson = new HashMap<String, Integer>();
			SalesPersonDao dao = new SalesPersonImp();
			SalesPerson[] allSalesPerson = null;
			try {
				allSalesPerson = dao.retrieveAll();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
			for(int i =0;i<allSalesPerson.length;i++)
			{
				SalesPerson temp=allSalesPerson[i];
				int value=temp.getAutoSalesPersonId();
				String key =temp.getSalesPersonName();
				SalesPerson.put(key, value);
			}
		}
		return SalesPerson.get(salesPersonName);
	}
	
	public static int getInfoSourceId(String InfoSourceName)
	{
		if(InfoSource == null)
		{
			
			InfoSource = new HashMap<String, Integer>();
			
			InfoSourceDao dao = new InfoSourceImp();
			InfoSource[] allInfoSource = null;
			try {
				allInfoSource = dao.retrieveAll();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
			for(int i =0;i<allInfoSource.length;i++)
			{
				InfoSource temp=allInfoSource[i];
				int value=temp.getAutoInfoSourceCode();
				String key =temp.getInfoSourceName();
				InfoSource.put(key, value);
			}
		}
		System.out.println(InfoSource+"--");
		return InfoSource.get(InfoSourceName);
	}
	
	public static String getProductCode(String productName)
	{
		if(Product == null)
		{
			Product = new HashMap<String,String>();
			ProductDao dao = new ProductImp();
			Product[] allProduct = null;
			try {
				allProduct = dao.retrieveAll();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
			for(int i =0;i<allProduct.length;i++)
			{
				Product temp=allProduct[i];
				String value=temp.getProductCode();
				String key =temp.getProductName();
				Product.put(key, value);
			}
		}
		return Product.get(productName);
	}
	
	public static int getCampaignId(String campaignName)
	{
		if(Campaign == null)
		{
			Campaign = new HashMap<String,Integer>();
			CampaignDao dao = new CampaignImp();
			Campaign[] allCampaign = null;
			try {
				allCampaign = dao.retrieveAll();
			} catch (Exception e) {
			
				e.printStackTrace();
			}
			
			for(int i =0;i<allCampaign.length;i++)
			{
				Campaign temp=allCampaign[i];
				String key=temp.getCampaignName();
				int value =temp.getAutocampaignId();
				Campaign.put(key, value);
			}
		}
		return Campaign.get(campaignName);
	}
/*----------------------------------------------------------------------------------------------------------------------*/

}
